package com.gome.oa.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import com.gome.oa.ExcelReader;
import com.gome.oa.TimeCalculator;
import com.gome.oa.dialog.FillFormDialog;
import com.gome.oa.dialog.PopupBrowserDialog;
import com.gome.oa.model.Day;
import com.gome.oa.model.OriginalData;
import com.gome.oa.util.SwingUtil;
import com.teamdev.jexplorer.Browser;

/**
 *
 * @author: tang li
 * @date: 2016年8月13日
 * @desc: 
 *
 */
public class FillFormActionListener implements ActionListener {
	
	private JTextField textFilePath;
	
	private Browser browser;
	
	private PopupBrowserDialog popupDialog;
	
	public FillFormActionListener(JTextField textFilePath, Browser browser, PopupBrowserDialog popupDialog) {
		super();
		this.textFilePath = textFilePath;
		this.browser = browser;
		this.popupDialog = popupDialog;
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		
		if(textFilePath.getText() == null || textFilePath.getText().trim().equals("")) {
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					JOptionPane.showConfirmDialog(popupDialog, "请先选择考勤记录表", "信息提示", JOptionPane.DEFAULT_OPTION);
				}
			});
			return;
		}

		File selectedFile = new File(textFilePath.getText());
		
		ExcelReader excelReader = new ExcelReader();
		
		try {
			List<OriginalData> list = excelReader.parse(selectedFile);
		       
			TimeCalculator calculator = new TimeCalculator();
			
			List<Day> days = calculator.calculate(list);
			
			final FillFormDialog fillFormDialog = new FillFormDialog(browser, popupDialog, days);
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					fillFormDialog.setVisible(true);
				}
			});
		} catch (Exception e1) {
			e1.printStackTrace();
			SwingUtil.alert(popupDialog, e1.getMessage() == null ? e1.toString() : e1.getMessage(), null);
		}
	}

}
